/**
 * @class   TelnetSession
 * @brief   S
 *
 *
 *
 *
 ****************************************************************************
 * @version $Id: TelnetSession.h 2880 2010-08-09 03:17:19Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Services_TelnetSession_H__
 #define __WOSH_Services_TelnetSession_H__

 #include <core/Thread.h>
 #include <core/Session.h>
 #include <framework/network/SocketTcp.h>

#define _TelnetSession_BUFFER		8191

using namespace wosh;

class TelnetSession;
class TelnetServerImpl;
namespace wosh {
 namespace services {
   class TelnetServerBundle;
  };
 };


class ITelnetSessionListener {
	public:
		virtual void session_connected( TelnetSession* session ) = 0;
		virtual void session_disconnected( TelnetSession* session ) = 0;
		virtual void session_error( TelnetSession* session ) = 0;

		virtual void session_registration( short reg_unreg, TelnetSession* session ) = 0;

	public:
		virtual ~ITelnetSessionListener () { }
};


class TelnetSession :	protected wosh::ThreadImpl,
						public wosh::Session,
						public virtual wosh::network::ISocketTcpListener
 {
	Q_OBJECT

	WOSH_SESSION(TelnetSession)

	public:
		TelnetSession();
		virtual ~TelnetSession();

/** @name Session Interface
 * @{
 ******************************************************************************/
	public:
		WRESULT open( unsigned long timeout = LIMIT_ULONG_MAX );
		WRESULT close( unsigned long timeout = LIMIT_ULONG_MAX );
//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		bool isConnected() const;
		inline int getSocketId() const					{ return this->socket_id; }
		inline wosh::network::SocketTcp* getSocket()	{ return this->tcpSocket; }

		static DataFieldCollector<TelnetSession>* getDataFields();

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		inline void setListener( ITelnetSessionListener* ptr )		{ this->listener = ptr; }

//@}
/** @name ISocketTcpListener
 * @{
 ******************************************************************************/
	public:
		void tcpDataAvailable( long data_len, wosh::network::SocketTcp* socket );
		void tcpSocketError( int error_code, const char* description, wosh::network::SocketTcp* socket );
//@}
	protected:
		void registered();
		void unregistered();

	protected:
		WRESULT printMessage( const std::string& output_text );

		bool handleLogin( bool& still_running );

		bool initializingThread();
		void runThread();
		void exitingThread();

	protected:
		wosh::network::SocketTcp* tcpSocket;
		int socket_id;

		bool writing;
		char* msgBufferOut;
		int msgBufferOutSize;
		wosh::MutexRW msgBufferOutMux;

		char* msgBufferIn;
		int msgBufferInSize;

		ITelnetSessionListener* listener;

		friend class TelnetServerImpl;
		friend class wosh::services::TelnetServerBundle;

	protected:
		std::string logging_user;
		std::string logging_pwd;

	private slots:
		void writeData();

	signals:
		void writeData_s();


}; // class def


#endif //__WOSH_Services_TelnetSession_H__
